package com.skyable.auth.config.security.filter;

import com.skyable.auth.entity.SysPermission;
import com.skyable.auth.service.IPermissionService;
import com.skyable.common.common.ServiceResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author GaoYuan
 * @title: BaseManager
 * @projectName private-cloud
 * @description: TODO
 * @date 2021/6/2118:30
 */
@Component
public class BaseManager {

    @Autowired
    private IPermissionService permissionService;

    protected List<GrantedAuthority> getUserAuthorityList(String userName) {
        List<GrantedAuthority> authorityList = new ArrayList<>();
        //查询用户权限
        ServiceResult<List<SysPermission>> permissionServiceResult = permissionService.findPermissionByUserName(userName);
        if (permissionServiceResult.getSuccess()) {
            List<SysPermission> sysPermissionList = permissionServiceResult.getResult();
            for (SysPermission sysPermission : sysPermissionList) {
                authorityList.add(new SimpleGrantedAuthority(sysPermission.getPermissionCode()));
            }
        }
        return authorityList;
    }

}
